package org.dromara.application.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.domain.dto.UserDTO;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.application.domain.vo.ApplicationVo;
import org.dromara.application.domain.bo.ApplicationBo;
import org.dromara.application.service.IApplicationService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 应用管理
 *
 * @author YCJZ
 * @date 2025-09-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/application/application")
public class ApplicationController extends BaseController {

    private final IApplicationService applicationService;

    /**
     * 查询应用管理列表
     */
    @SaCheckPermission("application:application:list")
    @GetMapping("/list")
    public TableDataInfo<ApplicationVo> list(ApplicationBo bo, PageQuery pageQuery) {
        return applicationService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取登录用户下的应用列表
     */
    @GetMapping("/getAppList")
    public R<List<ApplicationVo>> getAppList(Long appTypeId){
        return R.ok(applicationService.getAppList(appTypeId));
    }

    /**
     * 导出应用管理列表
     */
    @SaCheckPermission("application:application:export")
    @Log(title = "应用管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ApplicationBo bo, HttpServletResponse response) {
        List<ApplicationVo> list = applicationService.queryList(bo);
        ExcelUtil.exportExcel(list, "应用管理", ApplicationVo.class, response);
    }

    /**
     * 获取应用管理详细信息
     *
     * @param appId 主键
     */
    @SaCheckPermission("application:application:query")
    @GetMapping("/{appId}")
    public R<ApplicationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long appId) {
        return R.ok(applicationService.queryById(appId));
    }

    /**
     * 新增应用管理
     */
    @SaCheckPermission("application:application:add")
    @Log(title = "应用管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ApplicationBo bo) {
        return toAjax(applicationService.insertByBo(bo));
    }

    /**
     * 修改应用管理
     */
    @SaCheckPermission("application:application:edit")
    @Log(title = "应用管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ApplicationBo bo) {
        return toAjax(applicationService.updateByBo(bo));
    }

    /**
     * 删除应用管理
     *
     * @param appIds 主键串
     */
    @SaCheckPermission("application:application:remove")
    @Log(title = "应用管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{appIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] appIds) {
        return toAjax(applicationService.deleteWithValidByIds(List.of(appIds), true));
    }

    /**
     * 查询已分配用户应用列表
     */
//    @SaCheckPermission("application:application:list")
    @GetMapping("/authUser/allocatedList")
    public TableDataInfo<UserDTO> allocatedList(String clientId,String userName, String phonenumber, PageQuery pageQuery) {
        return applicationService.selectAllocatedList(clientId, userName, phonenumber, pageQuery);
    }

    /**
     * 查询未分配用户应用列表
     */
//    @SaCheckPermission("application:application:list")
    @GetMapping("/authUser/unallocatedList")
    public TableDataInfo<UserDTO> unallocatedList(String clientId,String userName, String phonenumber, PageQuery pageQuery) {
        return applicationService.selectUnallocatedList(clientId, userName, phonenumber, pageQuery);
    }

    /**
     * 批量取消授权用户
     * @param clientId clientId
     * @param userIds 用户ID串
     */
    @PutMapping("/authUser/cancelAll")
    public R<Void> cancelAuthUserAll(String clientId, Long[] userIds) {
        return toAjax(applicationService.deleteAuthUsers(clientId, userIds));
    }

    /**
     * 取消授权用户
     * @param clientId clientId
     * @param userId 用户ID
     */
    @PutMapping("/authUser/cancel")
    public R<Void> cancelAuthUser(String clientId, Long userId) {
        return toAjax(applicationService.deleteAuthUsers(clientId, new Long[]{userId}));
    }

    /**
     * 批量选择授权用户
     * @param clientId clientId
     * @param userIds 用户ID串
     */
    @PutMapping("/authUser/selectAll")
    public R<Void> selectAuthUserAll(String clientId, Long[] userIds) {
        return toAjax(applicationService.insertAuthUsers(clientId, userIds));
    }

}
